home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE16 / TIPTRIX / LISTING2.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-11-15  |  1.1 KB  |  41 lines

  1. { Stream for memory-mapped files }
  2. type
  3.   TMemoryFileStream = class(TFileStream)
  4.   private
  5.     FFileMapping : THandle;
  6.     FFileBase : Pointer;
  7.   public
  8.     constructor Create(const FileName: String; Mode: Word);
  9.     destructor Destroy; override;
  10.     property Memory: Pointer read FFileBase;
  11.   end;
  12. constructor TMemoryFileStream.Create(const FileName: String; Mode: Word);
  13. var
  14.   aFlag : Integer;
  15. begin
  16.   inherited Create(FileName, Mode);
  17.   if (Mode and fmOpenReadWrite) <> 0 then
  18.     aFlag := PAGE_READWRITE
  19.   else
  20.     aFlag := PAGE_READONLY;
  21.   FFileMapping := CreateFileMapping(Handle, nil, aFlag, 0, 0, nil);
  22.   if FFileMapping = 0 then
  23.     raise Exception.Create('CreateFileMapping failed');
  24.   if (Mode and fmOpenReadWrite) <> 0 then
  25.     aFlag := FILE_MAP_WRITE
  26.   else
  27.     aFlag := FILE_MAP_READ;
  28.   FFileBase := MapViewOfFile(FFileMapping, aFlag, 0, 0, 0);
  29.   if FFileBase = nil then
  30.     raise Exception.Create('MapViewOfFile failed');
  31. end;
  32.  
  33. destructor TMemoryFileStream.Destroy;
  34. begin
  35.  if FFileBase <> nil then
  36.    UnmapViewOfFile(FFileBase);
  37.  if FFileMapping <> 0 then
  38.    CloseHandle(FFileMapping);
  39.  inherited Destroy;
  40. end;
  41.